<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="投诉类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择投诉类型" clearable>
          <el-option
            v-for="dict in dict.type.feedback_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:feedback:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:feedback:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="司机姓名" align="center" prop="name" />
      <el-table-column label="车牌号" align="center" prop="carNo" />
      <el-table-column label="车辆整洁干净评分" align="center" prop="vehicleCleanlinessRating">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.vehicleCleanlinessRating"
            disabled
            show-score
            text-color="#ff9900">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="司机态度友好评分" align="center" prop="driverFriendlyRating">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.driverFriendlyRating"
            disabled
            show-score
            text-color="#ff9900">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="安全愉快到达评分" align="center" prop="safeAndPunctualRating">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.safeAndPunctualRating"
            disabled
            show-score
            text-color="#ff9900">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="反馈类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag v-if="scope.row.type" :options="dict.type.feedback_type" :value="scope.row.type"/>
          <span v-else>默认好评</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈内容" align="center" prop="complaints">
        <template slot-scope="scope">
          <span>{{ scope.row.complaints || '暂未填写' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投诉记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="司机姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="form.carNo" />
        </el-form-item>
        <el-form-item label="车辆整洁干净评分" prop="vehicleCleanlinessRating">
          <el-rate v-model="form.vehicleCleanlinessRating" show-score text-color="#ff9900"></el-rate>
        </el-form-item>
        <el-form-item label="司机态度友好评分" prop="driverFriendlyRating">
          <el-rate v-model="form.driverFriendlyRating" show-score text-color="#ff9900"></el-rate>
        </el-form-item>
        <el-form-item label="安全愉快到达评分" prop="safeAndPunctualRating">
          <el-rate v-model="form.safeAndPunctualRating" show-score text-color="#ff9900"></el-rate>
        </el-form-item>
        <el-form-item label="反馈类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择反馈类型">
            <el-option
              v-for="dict in dict.type.feedback_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="反馈内容" prop="complaints">
          <el-input v-model="form.complaints" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="投诉详情" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="司机姓名">{{ detailForm.name || '暂未填写' }}</el-descriptions-item>
        <el-descriptions-item label="车牌号">{{ detailForm.carNo || '暂未填写' }}</el-descriptions-item>
        <el-descriptions-item label="投诉类型">
          <dict-tag v-if="detailForm.type" :options="dict.type.feedback_type" :value="detailForm.type"/>
          <span v-else>默认好评</span>
        </el-descriptions-item>
        <el-descriptions-item label="车辆整洁干净评分">
          <el-rate v-model="detailForm.vehicleCleanlinessRating" disabled show-score text-color="#ff9900"></el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="司机态度友好评分">
          <el-rate v-model="detailForm.driverFriendlyRating" disabled show-score text-color="#ff9900"></el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="安全愉快到达评分">
          <el-rate v-model="detailForm.safeAndPunctualRating" disabled show-score text-color="#ff9900"></el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="投诉内容">{{ detailForm.complaints || '暂未填写' }}</el-descriptions-item>
        <el-descriptions-item label="投诉时间">{{ parseTime(detailForm.createTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="上传文件">
          <div v-if="detailForm.files && detailForm.files.length > 0">
            <div v-for="(file, index) in detailForm.files.split(',')" :key="index" class="file-item">
              <img :src="'https://suzhou-taxi-1306862033.cos.ap-chengdu.myqcloud.com/file/' + file" class="file-image" @click="previewImage(file)">
            </div>
          </div>
          <span v-else>暂未上传</span>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/system/feedback";

export default {
  name: "Feedback",
  dicts: ['feedback_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投诉记录表格数据
      feedbackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        complaints: null,
      },
      // 表单参数
      form: {
        id: null,
        driverName: null,
        licensePlate: null,
        vehicleCleanlinessRating: null,
        driverFriendlyRating: null,
        safeAndPunctualRating: null,
        type: null,
        complaints: null,
        files: null,
        createTime: null,
        updateTime: null
      },
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "投诉时间不能为空", trigger: "blur" }
        ],
      },
      // 详情对话框
      detailOpen: false,
      // 详情表单
      detailForm: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投诉记录列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        driverName: null,
        licensePlate: null,
        vehicleCleanlinessRating: null,
        driverFriendlyRating: null,
        safeAndPunctualRating: null,
        type: null,
        complaints: null,
        files: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投诉记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投诉记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除投诉记录编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.detailForm = row;
      this.detailOpen = true;
    },
    /** 预览图片 */
    previewImage(file) {
      window.open('https://suzhou-taxi-1306862033.cos.ap-chengdu.myqcloud.com/file/' + file);
    },
  }
};
</script>

<style scoped>
.file-item {
  display: inline-block;
  margin: 5px;
}
.file-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  cursor: pointer;
}
</style>
